﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReprojectWebMercator.Samples
{
    internal class gradientsTest:GLSLFuncBase
    {
        public sampler2D u_displacement;
        public sampler2D u_texture;
        public sampler2D u_mask;

        const float PI2 = 6.283185307179586f;
        const float PI = PI2 / 2;
        
        int u_typeId;
        float u_tick;
        int u_maskId;

        public override vec4 PixelPassMain(vec2 v_uv)
        {
            vec2 st = new vec2(0.3,0.3);

            vec4 displacement = texture2D(u_displacement, st);

            vec2 direction = vec2(cos(displacement.r * PI2), sin(displacement.r * PI2));
            float length = displacement.g;

            vec2 newUv = v_uv;

            newUv.x += (float)((length * 0.07) * direction.x);
            newUv.y += (float)((length * 0.07) * direction.y);

            vec4 texture = texture2D(u_texture, newUv);
            float tick = (float)(u_tick * 0.009);

            vec3 color = gradient3(u_typeId, v_uv, tick);

            texture.rgb = color + (texture.rgb * color);

            vec4 mask = texture2D(u_mask, st);

            int maskId = (int)(mask.r * 4.0 + mask.g * 2.0 + mask.b * 1.0);

            if (maskId == u_maskId)
            {
                return  vec4(texture.rgb, texture.a * mask.a);
            }
            else
            {
                return new vec4(0, 0, 0, 0);
            }
        }

        

        vec3 gradient3(int t,vec2 st, float tick)
        {
            vec3 c1 = vec3(0.89, 0.12, 0.78);
            vec3 c2 = vec3(0.29, 0.68, 0.95);

            st -= vec2(0.5);
            st *= scale(vec2(3.8));
            st *= rotate2d(tick * PI);
            st += vec2(0.5);

            return mix(c1, c2, st.x);
        }

        mat2 scale(vec2 value)
        {
            return mat2(value.x, 0.0, 0.0, value.y);
        }

        mat2 rotate2d(float value)
        {
            return mat2(cos(value), -sin(value), sin(value), cos(value));
        }

    }
}
